package cn.facetoken.homer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

/**
 * An activity representing a list of Items. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link DemonstrationActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 */
public class ItemListActivity extends AppCompatActivity {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean havingTwoPane;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_list);

        @NonNull final Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(getTitle());

        @NonNull final FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());

        if (findViewById(R.id.item_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-w900dp).
            // If this view is present, then the
            // activity should be in two-pane mode.
            havingTwoPane = true;
        }

        @NonNull final RecyclerView recyclerView = findViewById(R.id.item_list);
        setupRecyclerView(recyclerView);
    }

    private void setupRecyclerView(@NonNull final RecyclerView recyclerView) {
        recyclerView.setAdapter(new DemonstrationRecyclerViewAdapter(this, havingTwoPane));
    }

    public static class DemonstrationRecyclerViewAdapter
            extends RecyclerView.Adapter<DemonstrationRecyclerViewAdapter.ViewHolder>
            implements View.OnClickListener {

        DemonstrationRecyclerViewAdapter(@NonNull final ItemListActivity parent,
                                         final boolean twoPane) {
            this.parentActivity = parent;
            this.havingTwoPane = twoPane;
        }

        @NonNull
        private final ItemListActivity parentActivity;

        private final boolean havingTwoPane;

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
            @NonNull final View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_list_content, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
            @NonNull final Demonstration demonstration = Demonstration.values()[position];
            holder.idView.setText(demonstration.getTitleResourceId());
            holder.contentView.setText(demonstration.getDescriptionResourceId());
            holder.itemView.setTag(position);
            holder.itemView.setOnClickListener(this);
            holder.itemView.setVisibility(demonstration.isActive() ? View.VISIBLE : View.GONE);
        }

        @Override
        public int getItemCount() {
            return Demonstration.values().length;
        }

        @Override
        public void onClick(@NonNull final View view) {
            @NonNull final Demonstration item = Demonstration.values()[(Integer) view.getTag()];
            Log.i(getClass().getSimpleName(), String.format("onClick(): item = %s, ordinal = %d", item, item.ordinal()));

            if (havingTwoPane) {
                //final Bundle arguments = new Bundle();
                //arguments.putInt(Demonstration.ARGUMENT_DEMONSTRATION_ID, item.ordinal());

                @NonNull final Fragment fragment = item.newFragment();
                //fragment.setArguments(arguments);
                parentActivity.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, fragment)
                        .commit();
            } else {
                @NonNull final Context context = view.getContext();
                @NonNull final Intent intent = new Intent(context, DemonstrationActivity.class);
                intent.putExtra(Demonstration.ARGUMENT_DEMONSTRATION_ID, item.ordinal());
                context.startActivity(intent);
            }
        }

        static class ViewHolder extends RecyclerView.ViewHolder {

            ViewHolder(@NonNull final View view) {
                super(view);
                idView = view.findViewById(R.id.id_text);
                contentView = view.findViewById(R.id.content);
            }

            @NonNull
            final TextView idView;
            @NonNull
            final TextView contentView;
        }
    }

}